import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:mall/constant/string.dart';
import 'package:mall/utils/shared_preferences_util.dart';
import 'package:mall/utils/wechat_userinfo_model.dart';
import 'package:shared_preferences/shared_preferences.dart';

Dio dio;

class HttpUtil {
  // 工厂模式
  static HttpUtil get instance => _getInstance();

  static HttpUtil _httpUtil;

  static HttpUtil _getInstance() {
    if (_httpUtil == null) {
      _httpUtil = HttpUtil();
    }
    return _httpUtil;
  }

  HttpUtil() {
    BaseOptions options = BaseOptions(
      connectTimeout: 5000,
      receiveTimeout: 5000,
    );
    dio = new Dio(options);

    // dio.interceptors.add(InterceptorsWrapper(onRequest:
    //     (RequestOptions options, RequestInterceptorHandler handler) async {
    //   SharedPreferences sharedPreferences =
    //       await SharedPreferences.getInstance();
    //
    //   String token = sharedPreferences.getString(Strings.TOKEN);
    //   if (token != null) {
    //     options.headers[Strings.TOKEN] =
    //         sharedPreferences.getString(Strings.TOKEN);
    //   }
    // }));
    // dio.interceptors.add(InterceptorsWrapper(onRequest:
    //     (RequestOptions options, RequestInterceptorHandler handler) async {
    //   print("========================请求数据===================");
    //   print("url=${options.uri.toString()}");
    //   print("params=${options.data}");
    //   dio.lock();
    //   await SharedPreferencesUtils.getToken().then((token) {
    //     options.headers[Strings.TOKEN] = token;
    //   });
    //   dio.unlock();
    //   return options;
    // }, onResponse: (Response response, ResponseInterceptorHandler handler) {
    //   print("========================返回数据===================");
    //   print("code=${response.statusCode}");
    //   print("response=${response.data}");
    // }, onError: (DioError error, ErrorInterceptorHandler handler) {
    //   print("========================请求错误===================");
    //   print("message =${error.message}");
    // }));
  }

  Future get(String url,
      {Map<String, dynamic> parameters, Options options}) async {
    Response response;
    if (parameters != null && options != null) {
      response =
          await dio.get(url, queryParameters: parameters, options: options);
    } else if (parameters != null && options == null) {
      response = await dio.get(url, queryParameters: parameters);
    } else if (parameters == null && options != null) {
      response = await dio.get(url, options: options);
    } else {
      response = await dio.get(url);
    }
    return response.data;
  }

  Future post(String url,
      {Map<String, dynamic> parameters, Options options}) async {
    Response response;
    if (parameters != null && options != null) {
      response = await dio.post(url, data: parameters, options: options);
    } else if (parameters != null && options == null) {
      response = await dio.post(url, data: parameters);
    } else if (parameters == null && options != null) {
      response = await dio.post(url, options: options);
    } else {
      response = await dio.post(url);
    }
    return response.data;
  }
}

// 获取微信用户信息
Future<WechatUserinfoModel> getWechatUserinfo(String code) async {
  // 获取信息
  Response response = await Dio().get(
      "https://api.weixin.qq.com/sns/oauth2/access_token?appid=${'wx04a32a525026be31'}&secret=${'cde58201d02f1cfb06a12ceac9f60f0f'}&code=$code&grant_type=authorization_code");
  dynamic data = json.decode(response.data);
  String openid = data["openid"];
  String accessToken = data["access_token"];

  Response userinfoResponse = await Dio().get(
      "https://api.weixin.qq.com/sns/userinfo?access_token=$accessToken&openid=$openid");
  dynamic userinfoJson = json.decode(userinfoResponse.data);
  var resultModel = WechatUserinfoModel.fromJson(userinfoJson);
  return resultModel;
}
